<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="medium"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item prop="paperName">
        <el-input
          v-model="queryParams.paperName"
          placeholder="请输入考试名称/描述"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="medium" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="medium" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['exam:paper:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="6">
        <div class="inner-select">
          <span>考试状态：</span>
          <el-select
            v-model="queryParams.paperStatus"
            placeholder="请选择"
            size="small"
            @change="changePaperStatus"
            :clearable="true"
          >
            <el-option
              v-for="dict in dict.type.paper_status"
              :value="dict.value"
              :label="dict.label"
            ></el-option>
          </el-select>
        </div>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="paperList">
      <el-table-column label="试卷名称" align="center" prop="paperName" :show-overflow-tooltip="true" />
      <el-table-column label="状态" align="center" prop="paperStatus">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="考试的状态" placement="top">
            <dict-tag :options="dict.type.paper_status" :value="scope.row.paperStatus" />
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="当前总分" align="center" prop="paperNowScore">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="通过调整试卷每道题目的分数来改变试卷当前总分" placement="top">
            <span>{{scope.row.paperNowScore}}</span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="合格分数" align="center" prop="paperPassScore">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="考试合格的最低分数" placement="top">
            <span>{{scope.row.paperPassScore}}</span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="考试时长" align="center" prop="paperDuration">
        <template slot-scope="scope">
          <span>{{scope.row.paperDuration}}分钟</span>
        </template>
      </el-table-column>
      <el-table-column label="题目是否乱序" align="center" prop="paperQuUpset">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="考试时每个人题目的顺序将被打乱" placement="top">
            <dict-tag :options="dict.type.paper_upset" :value="scope.row.paperQuUpset" />
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="选项是否乱序" align="center" prop="paperOptionsUpset">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="考试时每个人题目选项的顺序将被打乱" placement="top">
            <dict-tag :options="dict.type.paper_upset" :value="scope.row.paperOptionsUpset" />
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column
        label="开始时间"
        align="center"
        prop="paperStartTime"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.paperStartTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="结束时间"
        align="center"
        prop="paperEndTime"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.paperEndTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['exam:paper:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['exam:paper:remove']"
          >删除</el-button>
          <el-button size="mini" type="text" icon="el-icon-info" @click="handleInfo(scope.row)">详细信息</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-data"
            @click="handleBindQuList(scope.row)"
            v-hasPermi="['exam:paper:list']"
          >题目列表</el-button>
          <el-button
            type="text"
            size="mini"
            icon="el-icon-user"
            @click="handleBindUserList(scope.row)"
            v-hasPermi="['exam:paper:list']"
          >用户列表</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改试卷对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="试卷名称" prop="paperName">
          <el-input v-model="form.paperName" placeholder="请输入试卷名称" />
        </el-form-item>
        <el-form-item label="试卷描述" prop="paperDesc">
          <el-input v-model="form.paperDesc" type="textarea" placeholder="请输入试卷描述" />
        </el-form-item>
        <el-form-item label="考试状态" prop="paperStatus">
          <el-select v-model="form.paperStatus" placeholder="请选择考试状态">
            <el-option
              v-for="dict in dict.type.paper_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开始时间" prop="paperStartTime">
          <el-date-picker
            clearable
            v-model="form.paperStartTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择考试开始时间"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="paperEndTime">
          <el-date-picker
            clearable
            v-model="form.paperEndTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择考试结束时间"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="合格分数" prop="paperPassScore">
          <el-input v-model="form.paperPassScore" placeholder="请输入合格分数" type="number" />
        </el-form-item>
        <el-form-item label="考试时长" prop="paperDuration">
          <el-input v-model="form.paperDuration" type="number" placeholder="请输入考试时长">
            <template slot="append">分钟</template>
          </el-input>
        </el-form-item>
        <el-form-item label="题目是否乱序" prop="paperQuUpset">
          <el-radio-group v-model="form.paperQuUpset">
            <el-radio
              v-for="dict in dict.type.paper_upset"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="选项是否乱序" prop="paperOptionsUpset">
          <el-radio-group v-model="form.paperOptionsUpset">
            <el-radio
              v-for="dict in dict.type.paper_upset"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 试卷详细信息 -->
    <paper-info :paperData="infoDialog.paperData" :visible.sync="infoDialog.paperShow" />

    <!-- 题库已绑定题目列表 -->
    <bind-qu-list
      :getBindQuList="listBindQu"
      :removeBindQu="removePaperQu"
      :bindId="bindQuId"
      :dialogAttr="bindQuDialog"
      @openUnBindList="handleUnBindQuList"
      :hasScore="true"
      :updateScore="updateQuScore"
    />
    <!-- 题库未绑定题目列表 -->
    <un-bind-qu-list
      :getUnBindQuList="listUnBindQu"
      :addUnBindQu="addPaperQu"
      :unBindId="unBindQuId"
      :dialogAttr="unBindQuDialog"
      :hasScore="true"
    />

    <!-- 用户已绑定用户列表 -->
    <bind-user-list
      :getBindUserList="listBindUser"
      :removeBindUser="removePaperUser"
      :bindId="bindUserId"
      :dialogAttr="bindUserDialog"
      @openUnBindList="handleUnBindUserList"
    />
    <!-- 用户未绑定用户列表 -->
    <un-bind-user-list
      :getUnBindUserList="listUnBindUser"
      :addUnBindUser="addPaperUser"
      :unBindId="unBindUserId"
      :dialogAttr="unBindUserDialog"
    />
  </div>
</template>

<script>
import {
  listPaper,
  getPaper,
  delPaper,
  addPaper,
  updatePaper,
  listBindQu,
  listUnBindQu,
  updateQuScore,
  addPaperQu,
  removePaperQu,
  listBindUser,
  listUnBindUser,
  addPaperUser,
  removePaperUser
} from "@/api/exam/paper";
import PaperInfo from "@/components/PaperInfo/paperInfo.vue";
import BindQuList from "@/components/QuInfo/bindQuList.vue";
import UnBindQuList from "@/components/QuInfo/unBindQuList.vue";
import BindUserList from "@/components/User/bindUserList.vue";
import UnBindUserList from "@/components/User/unBindUserList.vue";
export default {
  name: "Paper",
  dicts: ["paper_status", "paper_upset"],
  components: {
    PaperInfo,
    BindQuList,
    UnBindQuList,
    BindUserList,
    UnBindUserList
  },
  data() {
    return {
      // 试卷id
      paperId: 0,
      // 绑定用户弹框属性
      bindUserDialog: {
        title: "",
        show: false
      },
      // 未绑定用户弹框属性
      unBindUserDialog: {
        title: "",
        show: false
      },
      // 用户id属性
      bindUserId: {
        type: "paperId",
        value: 0
      },
      unBindUserId: {
        type: "paperId",
        value: 0
      },
      // 绑定题库弹框属性
      bindQuDialog: {
        title: "",
        show: false
      },
      // 未绑定题库弹框属性
      unBindQuDialog: {
        title: "",
        show: false
      },
      // 题库id属性
      bindQuId: {
        type: "paperId",
        value: 0
      },
      unBindQuId: {
        type: "paperId",
        value: 0
      },
      // 绑定用户函数
      listBindUser,
      removePaperUser,
      // 未绑定用户函数
      listUnBindUser,
      addPaperUser,
      // 绑定题库函数
      listBindQu,
      removePaperQu,
      updateQuScore,
      // 未绑定题库函数
      listUnBindQu,
      addPaperQu,
      // infoDialog
      infoDialog: {
        // 试卷信息
        paperShow: false,
        paperData: {}
      },
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 试卷表格数据
      paperList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        paperName: undefined,
        paperStatus: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        paperName: [
          { required: true, message: "考试名称不能为空", trigger: "blur" }
        ],
        paperStatus: [
          { required: true, message: "考试状态不能为空", trigger: "change" }
        ],
        paperStartTime: [
          { required: true, message: "开始时间不能为空", trigger: "blur" }
        ],
        paperEndTime: [
          { required: true, message: "结束时间不能为空", trigger: "blur" }
        ],
        paperPassScore: [
          { required: true, message: "合格分数不能为空", trigger: "blur" }
        ],
        paperDuration: [
          { required: true, message: "考试时长不能为空", trigger: "blur" }
        ],
        paperQuUpset: [
          { required: true, message: "题目是否乱序不能为空", trigger: "blur" }
        ],
        paperOptionsUpset: [
          { required: true, message: "选项是否乱序不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询试卷列表 */
    getList() {
      this.loading = true;
      listPaper(this.queryParams).then(response => {
        this.paperList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        paperId: undefined,
        paperName: undefined,
        paperDesc: undefined,
        paperStatus: 0,
        paperStartTime: undefined,
        paperEndTime: undefined,
        paperPassScore: undefined,
        paperDuration: undefined,
        paperQuUpset: 0,
        paperOptionsUpset: 0
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.paperStatus = undefined;
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加试卷";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const paperId = row.paperId;
      getPaper(paperId).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改试卷";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.paperId != null) {
            updatePaper(this.form)
              .then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          } else {
            addPaper(this.form)
              .then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const paperIds = row.paperId;
      this.$modal
        .confirm('是否确认删除试卷编号为"' + paperIds + '"的数据项？')
        .then(() => {
          this.loading = true;
          return delPaper(paperIds);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /** 更改试卷状态搜索 */
    changePaperStatus() {
      this.getList();
    },
    /** 详细信息 */
    handleInfo(row) {
      this.infoDialog.paperData = row;
      this.infoDialog.paperShow = true;
    },
    /** 已绑定题目列表 */
    handleBindQuList(row) {
      this.bindQuDialog.title = "已绑定题目列表!";
      this.bindQuDialog.show = true;
      this.bindQuId.value = row.paperId;
      // paperId
      this.paperId = row.paperId;
    },
    /** 弹出试卷未绑定题目列表 */
    handleUnBindQuList() {
      this.unBindQuDialog.title = "未绑定题目列表!";
      this.unBindQuDialog.show = true;
      this.unBindQuId.value = this.paperId;
    },
    /** 已绑定用户列表 */
    handleBindUserList(row) {
      this.bindUserDialog.title = "已绑定用户列表!";
      this.bindUserDialog.show = true;
      this.bindUserId.value = row.paperId;
      // paperId
      this.paperId = row.paperId;
    },
    /** 弹出试卷未绑定用户列表 */
    handleUnBindUserList() {
      this.unBindUserDialog.title = "未绑定用户列表!";
      this.unBindUserDialog.show = true;
      this.unBindUserId.value = this.paperId;
    }
  }
};
</script>
<style scoped>
.bindBtn {
  margin-left: 10px;
}
.inner-select {
  display: flex;
  justify-content: center;
}
.inner-select span {
  font-size: 16px;
  color: #909399;
  line-height: 32px;
}
.drawer_el_row {
  margin: 0 !important;
  margin-bottom: 10px !important;
}
</style>
